package epoch.game.core.message;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import epoch.game.core.common.Loggers;
import epoch.game.core.serialize.ISerializable;
import epoch.game.core.serialize.StreamWriter;
import epoch.game.core.server.ISession;

/**
 * 抽象的消息类
 * 
 * @author will
 *
 */
public abstract class Message implements ISerializable, IMessage {
	
	private static final long serialVersionUID = 8358861993968887937L;
	
	//发送对象
	private long sender;
	//接受者
	private List<Long> receivers = new ArrayList<Long>();
	//会话
	private ISession session;
	//参数
	private Object param;

	public abstract short getId();

	@Override
	public long getSender() {
		return sender;
	}

	@Override
	public void setSender(long sender) {
		this.sender = sender;
	}

	@Override
	public List<Long> getReceivers() {
		return receivers;
	}

	@Override
	public void setReceivers(List<Long> receivers) {
		this.receivers = receivers;
	}

	public ISession getSession() {
		return session;
	}

	public void setSession(ISession session) {
		this.session = session;
	}

	@Override
	public String getQueue() {
		return null;
	}

	@Override
	public String getServer() {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getParameter() {
		return (T) param;
	}

	@Override
	public <T> void setParameter(T param) {
		this.param = param;
	}
	
	public byte[] getBytes(){
		StreamWriter writer  = new StreamWriter();
		try {
			writeTo(writer);
		} catch (IOException ex) {
			Loggers.MSG_LOGGER.error("msg serialize err! ex="+ex, ex);
			throw new RuntimeException("msg serialize err!"+this.getClass(), ex);
		}
		return writer.array();
	}

}
